package xiaohongshu

import (
	"marketing-server/global"
	"marketing-server/model/marketing/xiaohongshu"
	xiaohongshuReq "marketing-server/model/marketing/xiaohongshu/request"
)

type XhsAccessTokenService struct{}

// CreateXhsAccessToken 创建xhsAccessToken表记录
// Author [yourname](https://github.com/yourname)
func (xhsAccessTokenService *XhsAccessTokenService) CreateXhsAccessToken(xhsAccessToken *xiaohongshu.XhsAccessToken) (err error) {
	err = global.MustGetGlobalDBByDBName("xiaohongshuAlias").Create(xhsAccessToken).Error
	return err
}

// DeleteXhsAccessToken 删除xhsAccessToken表记录
// Author [yourname](https://github.com/yourname)
func (xhsAccessTokenService *XhsAccessTokenService) DeleteXhsAccessToken(id string) (err error) {
	err = global.MustGetGlobalDBByDBName("xiaohongshuAlias").Delete(&xiaohongshu.XhsAccessToken{}, "id = ?", id).Error
	return err
}

// DeleteXhsAccessTokenByIds 批量删除xhsAccessToken表记录
// Author [yourname](https://github.com/yourname)
func (xhsAccessTokenService *XhsAccessTokenService) DeleteXhsAccessTokenByIds(ids []string) (err error) {
	err = global.MustGetGlobalDBByDBName("xiaohongshuAlias").Delete(&[]xiaohongshu.XhsAccessToken{}, "id in ?", ids).Error
	return err
}

// UpdateXhsAccessToken 更新xhsAccessToken表记录
// Author [yourname](https://github.com/yourname)
func (xhsAccessTokenService *XhsAccessTokenService) UpdateXhsAccessToken(xhsAccessToken xiaohongshu.XhsAccessToken) (err error) {
	err = global.MustGetGlobalDBByDBName("xiaohongshuAlias").Model(&xiaohongshu.XhsAccessToken{}).Where("id = ?", xhsAccessToken.Id).Updates(&xhsAccessToken).Error
	return err
}

// GetXhsAccessToken 根据id获取xhsAccessToken表记录
// Author [yourname](https://github.com/yourname)
func (xhsAccessTokenService *XhsAccessTokenService) GetXhsAccessToken(id string) (xhsAccessToken xiaohongshu.XhsAccessToken, err error) {
	err = global.MustGetGlobalDBByDBName("xiaohongshuAlias").Where("id = ?", id).First(&xhsAccessToken).Error
	return
}

// GetXhsAccessTokenInfoList 分页获取xhsAccessToken表记录
// Author [yourname](https://github.com/yourname)
func (xhsAccessTokenService *XhsAccessTokenService) GetXhsAccessTokenInfoList(info xiaohongshuReq.XhsAccessTokenSearch) (list []xiaohongshu.XhsAccessToken, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.MustGetGlobalDBByDBName("xiaohongshuAlias").Model(&xiaohongshu.XhsAccessToken{})
	var xhsAccessTokens []xiaohongshu.XhsAccessToken
	// 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&xhsAccessTokens).Error
	return xhsAccessTokens, total, err
}
func (xhsAccessTokenService *XhsAccessTokenService) GetXhsAccessTokenPublic() {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
